package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.AdminGuidelineDto;
import com.wxzz.elearing.user.dto.WorkGuidelineDto;
import com.wxzz.elearing.user.entity.AdminUser;
import com.wxzz.elearing.user.entity.AdminWorkGuideline;
import com.wxzz.elearing.user.entity.AdminWorkNode;
import com.wxzz.elearing.user.entity.Enterprise;
import com.wxzz.elearing.user.mapper.AdminUserMapper;
import com.wxzz.elearing.user.mapper.AdminWorkGuidelineMapper;
import com.wxzz.elearing.user.mapper.AdminWorkNodeMapper;
import com.wxzz.elearing.user.mapper.EnterpriseMapper;
import com.wxzz.elearing.user.service.IAdminWorkGuidelineService;
import com.wxzz.elearing.user.vo.AdminWorkGuidelineVo;
import com.wxzz.elearing.user.vo.AdminWorkNodeVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2021-05-16
 */
@Service
public class AdminWorkGuidelineServiceImpl extends ServiceImpl<AdminWorkGuidelineMapper, AdminWorkGuideline> implements IAdminWorkGuidelineService {

    @Autowired
    private AdminWorkGuidelineServiceImpl adminWorkGuidelineService;

    @Resource
    private AdminWorkGuidelineMapper adminWorkGuidelineMapper;

    @Resource
    private AdminWorkNodeMapper adminWorkNodeMapper;
    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private EnterpriseMapper enterpriseMapper;

    public ReturnVO addAdminGuideline(AdminWorkGuideline adminWorkGuideline){

        adminWorkGuidelineService.save(adminWorkGuideline);
        return new ReturnVO("工作指引添加完成",null);
    }

    public ReturnVO<JgPageVo<List<AdminWorkGuidelineVo>>> getAdminGuidelineList(AdminGuidelineDto adminGuidelineDto){
        Page<Object> objects = PageHelper.startPage(adminGuidelineDto.getCurPage(), adminGuidelineDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(adminGuidelineDto.getWorkGuidelineName())){
            queryWrapper.like("work_guideline_name",adminGuidelineDto.getWorkGuidelineName()+"%");
        }
        queryWrapper.eq("enterprise_id",adminGuidelineDto.getEnterpriseId());
        queryWrapper.orderByDesc("create_time");
        List<AdminWorkGuideline> list = adminWorkGuidelineService.list(queryWrapper);
        List<AdminWorkGuidelineVo> adminWorkGuidelineVoList = new ArrayList();
        list.forEach((AdminWorkGuideline adminWorkGuideline)->{
            AdminWorkGuidelineVo adminWorkGuidelineVo = new AdminWorkGuidelineVo();
            AdminUser adminUser = adminUserMapper.selectById(adminWorkGuideline.getAdminUserId());
            Optional.ofNullable(adminUser).ifPresent(a-> adminWorkGuidelineVo.setAdminUserName(adminUser.getUsername()));
            Enterprise enterprise = enterpriseMapper.selectById(adminWorkGuideline.getEnterpriseId());
            Optional.ofNullable(enterprise).ifPresent(e->  adminWorkGuidelineVo.setEnterpriseName(enterprise.getEnterpriseName()));
            BeanUtils.copyProperties(adminWorkGuideline,adminWorkGuidelineVo);
            adminWorkGuidelineVoList.add(adminWorkGuidelineVo);
        });
        long total = objects.getTotal();
        PageInfo pageInfo = new PageInfo(adminWorkGuidelineVoList);
        pageInfo.setTotal(total);
        return new ReturnVO("工作指引列表查询成功",new JgPageVo<>(pageInfo));
//        return new ReturnVO("工作指引列表查询成功", new JgPageVo(new PageInfo<>(adminWorkGuidelineVoList)));
    }
    public ReturnVO deleteWorkGuide(WorkGuidelineDto workGuidelineDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("work_guideline_id",workGuidelineDto.getWorkGuidelineId());
        Integer integer = adminWorkNodeMapper.selectCount(queryWrapper);
        if(integer!=0){
            throw new ServiceException("该工作指引有子节点,请先删除子节点后继续操作！");
        }
        adminWorkGuidelineMapper.deleteById(workGuidelineDto.getWorkGuidelineId());
        return new ReturnVO("删除指引成功",null);
    }
}
